package com.mall.controller;


import com.mall.entity.GoodsVisit;
import com.mall.pojo.ResponseMessage;
import com.mall.service.IGoodsVisitService;
import com.mall.util.ResponseUtil;
import com.mall.util.UserUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 商品访问历史表 前端控制器
 * </p>
 * @author ShouPeng
 * @since 2024-01-19
 */
@RestController
@RequestMapping("/user/goods/visit")
@CrossOrigin
public class GoodsVisitController {
    final
    IGoodsVisitService goodsVisitService;

    public GoodsVisitController(IGoodsVisitService goodsVisitService) {
        this.goodsVisitService = goodsVisitService;
    }

    @GetMapping
    public ResponseMessage queryGoodsVisitRecord(HttpServletRequest request){
        return ResponseMessage.success(goodsVisitService.queryGoodsVisitRecord(UserUtils.userId(request)));
    }

    @PostMapping
    public ResponseMessage addVisitRecord(@RequestBody GoodsVisit visit, HttpServletRequest request){
        visit.setUserId(UserUtils.userId(request));
        return goodsVisitService.addVisitRecord(visit) ? ResponseMessage.success() : ResponseMessage.custom("-1","添加商品访问记录失败！");
    }

    @DeleteMapping
    public ResponseMessage modifyGoodsHistory( String visitId) {
        System.out.println("用户请求:"+visitId);
        if (visitId == null) {
            return ResponseUtil.fail("数据传入失败");
        }

        int i = goodsVisitService.removeVisitHistory(visitId);
        if (i == 0) {
            return ResponseUtil.databaseError();
        }
        return ResponseUtil.success();
    }

    @DeleteMapping("/all")
    public ResponseMessage clearGoodsHistory(HttpServletRequest request) {
        return ResponseMessage.success(goodsVisitService.clearGoodsHistory(UserUtils.userId(request)));
    }
}


